﻿#include <time.h>
#include <d3d9.h>
#include <d3dx9.h>

#include "CastleVaniaGame.h"
#include "Surface.h"
#include "Resource.h"
#include "Collision.h"
#include "Ghost.h"
#include "RedBat.h"
#include "RedDragon.h"
#include "HeadGhost.h"

/**--------------------------------------------------------
*Description : Constructor
*Method name : MarioManager
*Parameters :  HINSTANCE hInstance, LPWSTR Name, int Mode, int IsFullScreen, int FrameRate
*Return value :
--------------------------------------------------------**/
CastleVaniaGame::CastleVaniaGame(HINSTANCE hInstance, LPWSTR Name, int Mode, int IsFullScreen, int FrameRate):
Game(hInstance,Name,Mode,IsFullScreen, FrameRate)
{
	//TODO: Init game
	quadtree = new QuadTree();
	//quadtree_tiles = new QuadTree(0, 0, 4096, 5);
	quadtree_tiles = new QuadTree();
}


/**--------------------------------------------------------
*Description : Destructor
*Method name : ~MarioManager
*Parameters :  
*Return value :
--------------------------------------------------------**/
CastleVaniaGame::~CastleVaniaGame()
{
	//TODO: Release game
	delete camera;
	delete load_quadtree_obj;
	delete quadtree;

	//delete load_quadtree_tiles;
	delete quadtree_tiles;


	delete dxAudio;

	delete background_sound;
	delete rod_sound;
	delete eat_item; 
	delete fireballSound; 

	/*delete bumpSound;
	delete coinSound; 
	delete deathSound;	
	delete gameOverSound;
	delete jumpSound; 
	delete kickSound; 
	delete lifeSound; 
	delete mipSound; 
	delete pipeWarpSound; 
	delete powerupSound; 
	delete startGameSound; 
	delete winGameSound;*/
}


/**--------------------------------------------------------
*Description : Load tai nguyen 
*Method name : LoadResources
*Parameters :  LPDIRECT3DDEVICE9 d3ddv
*Return value :void
--------------------------------------------------------**/
void CastleVaniaGame::LoadResources(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 backBuffer)
{
	HRESULT res = D3DXCreateSprite(d3ddv, &_spriteHandler);
	//TODO: Load resource game	
	ResourceMng *rm = ResourceMng::GetInst(d3ddv, backBuffer);

	 
	count_time = 400;

	reset_time = 0;

	map_current = 11;
	is_change_map = false;

	submap = 0;
	subMapToMap = 0;
	is_replay = false;

	is_load_game = 0;

	camera = new Camera();
	width_map = 0;

	//load map (obj, tiles)
	load_quadtree_obj = new LoadMap(d3ddv, backBuffer, _spriteHandler);
	
	game_state = GS_MENU;

	//pointMenu = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\star.png"), 125, 0);
	pointMenu = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[30]->getFileName()), 125, 0);
	xPointMenu = 200;
	yPointMenu = 270;

	simon_axe_version = WEAPON_NONE;
	simon_rod_version = 1;
	simon_blood = 8;
	boss_blood = 8;


	/*
	* info score, info coin, info number life, info time, info world1, info world2
	*/
	scoreArr[SCORE] = new Score(d3ddv, backBuffer, _spriteHandler, 200, 480);			 
	scoreArr[HEART] = new Score(d3ddv, backBuffer, _spriteHandler, 400, 450);			 
	scoreArr[HEART]->Update(num_heart);
	scoreArr[NUMBER_LIFE] = new Score(d3ddv, backBuffer, _spriteHandler, 400, 430);		 
	scoreArr[NUMBER_LIFE]->Update(num_life);
	scoreArr[TIME] = new Score(d3ddv, backBuffer, _spriteHandler, 360, 480);
	scoreArr[WORLD1] = new Score(d3ddv, backBuffer, _spriteHandler, 480, 480);			 
	scoreArr[WORLD1]->Update(map_current);
	

	/*
	info num life, info coin, info world, info time, info result
	*/
	

	infoArr[0] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[26]->getFileName()), 125, 0), 20, 480);	
	infoArr[1] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[25]->getFileName()), 125, 0), 350, 450);	
	infoArr[2] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[29]->getFileName()), 125, 0), 400, 480);	
	infoArr[3] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[27]->getFileName()), 125, 0), 250, 480);
	infoArr[4] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[28]->getFileName()), 125, 0), 350, 430);	
	infoArr[5] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[38]->getFileName()), 125, 0), 20, 450);	
	infoArr[6] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[37]->getFileName()), 125, 0), 20, 430);	
	infoArr[7] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[39]->getFileName()), 125, 0), 270, 450);

	for (int i = 0; i < 8; i++)
	{
		info_death_player[i] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[35]->getFileName()), 0, 0), 120 + i*16, 450);
		info_death_boss[i] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[35]->getFileName()), 0, 0), 120 + i*16, 430);
	}
	for (int i = 0; i < 8; i++)
	{
		info_blood_player[i] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[34]->getFileName()), 0, 0), 120 + i*16, 450);
		info_blood_boss[i] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[36]->getFileName()), 0, 0), 120 + i*16, 430);
	}
	
	for (int i = 0; i < 5; i++)
	{
		Sprite* p_sprite_temp = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[20]->getFileName()), 0, 0);
		p_sprite_temp->setFrame(0,1);
		p_sprite_temp->setIndex(0);
		if (i == 1)
		{
			p_sprite_temp = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[20]->getFileName()), 0, 0);
			p_sprite_temp->setFrame(4,5);
			p_sprite_temp->setIndex(4);
		}
		else if (i == 2)
		{
			p_sprite_temp = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[20]->getFileName()), 0, 0);
			p_sprite_temp->setFrame(5,6);
			p_sprite_temp->setIndex(5);
		}
		else if (i == 3)
		{
			p_sprite_temp = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[20]->getFileName()), 0, 0);
			p_sprite_temp->setFrame(8,9);
			p_sprite_temp->setIndex(8);
		}
		else if (i == 4)
		{
			p_sprite_temp = new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, backBuffer)->getMapTexture()[20]->getFileName()), 0, 0);
			p_sprite_temp->setFrame(15,16);
			p_sprite_temp->setIndex(15);
		}
		info_weapon[i] = new Info(p_sprite_temp, 285, 450);
	}

	
	/*infoArr[0] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infomario.png"), 0, 0), 20, 480);	
	infoArr[1] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infocoin.png"), 0, 0), 350, 450);	
	infoArr[2] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infoworld.png"), 0, 0), 400, 480);	
	infoArr[3] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\infotime.png"), 0, 0), 250, 480);	*/
	//infoArr[4] = new Info(new Sprite(_spriteHandler, ResourceMng::GetInst(d3ddv, backBuffer)->GetTexture(L"..\\Resources\\Images\\inforesult.png"), 0, 0), 350, 430);
	
	

	

	  
	//load bounce wave file
	dxAudio = new DxAudio();
	dxAudio->Init_DirectSound(_hWnd);

	background_sound = dxAudio->LoadSound(L"..\\Resources\\Audio\\background_sound.wav");
	rod_sound = dxAudio->LoadSound(L"..\\Resources\\Audio\\rod_sound.wav");
	eat_item = dxAudio->LoadSound(L"..\\Resources\\Audio\\eat_item.wav");


	fireballSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Fireball.wav");

	/*bumpSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Bump.wav");
	coinSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Coin.wav");
	deathSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Death.wav");
	
	gameOverSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\GameOver.wav");
	jumpSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Jump.wav");
	kickSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Kick.wav");
	lifeSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Life.wav");
	mipSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\Mip.wav");
	pipeWarpSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\PipeWarp.wav");
	powerupSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\PowerUp.wav");
	startGameSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\StartGame.wav");
	winGameSound = dxAudio->LoadSound(L"..\\Resources\\Audio\\WinStage.wav");*/


}


/**--------------------------------------------------------
*Description : Render Frame
*Method name : RenderFrame
*Parameters :  LPDIRECT3DDEVICE9 d3ddv, int t
*Return value :void
--------------------------------------------------------**/
void CastleVaniaGame::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 backBuffer, int t)
{
	//TODO: DRAW---------------------------------------------------------
	// Surface
	//GLDevice->StretchRect(...)
	_spriteHandler->Begin(D3DXSPRITE_ALPHABLEND);


	switch (game_state)
	{
	case GS_MENU:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\menu.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[2])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		pointMenu->render(xPointMenu, yPointMenu);
		break;

	case GS_ABOUT:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\about.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[3])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		//pointMenu->Render(xPointMenu, yPointMenu);
		break;

	case GS_PAUSE:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[6])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[6])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		pointMenu->render(xPointMenu, yPointMenu);
		//infoArr[6]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		//infoArr[7]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		//infoArr[8]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		break;


	case GS_GAMEOVER:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\gameover.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[4])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		//4
		break;

	case GS_GAMEWIN:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\gamewin.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[5])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\gamewin.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		//pointMenu->Render(xPointMenu, yPointMenu);
		break;

	case GS_SUB_STAGE:
		break;

	case GS_RETURN_STAGE:
		break;

	case GS_REPLAY:
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[1])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		break;

	case GS_NEXT_STAGE:
		//d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);		
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\background.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[7])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL

		/*scoreArr[SCORE]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[COIN]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[NUMBER_LIFE]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[WORLD1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[WORLD2]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);

		infoArr[0]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[2]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[3]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[4]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[5]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);*/

		//pointMenu->Render(xPointMenu, yPointMenu);
		break;

	case GS_LOAD:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		break;

	case GS_PLAYING:
		d3ddv->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0,0);
		//ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(L"..\\Resources\\Images\\background.jpg")->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL
		ResourceMng::GetInst(d3ddv, backBuffer)->GetSurface(ResourceMng::GetInst(d3ddv, backBuffer)->getMapSurface()[1])->Draw(NULL, NULL ); //chu y ham Draw co 2 tham so NULL

		//Draw tiles background
		RenderBackground();

		Object *obj;
		list<Object*>::iterator i;

		//draw obj land first
		for (i = obj_list.begin(); i != obj_list.end(); i++)
		{
			obj = *i;
			if (obj->getObjectType() == OBJECT_SCENE || obj->getObjectType() == OBJECT_LAND || obj->getObjectType() == OBJECT_STAIR)
			{
				if (obj->getPosX() > camera->getCamera().left - 30 && obj->getPosX() < camera->getCamera().left + _ScreenWidth + 30)
				{
					if (obj->getIDObjType() != 2411)
					{
						obj->render(camera->getCamera().left, camera->getCamera().top);
					}
				}
			}			
		}

		//then draw obj enemy 
		for (i = obj_list.begin(); i != obj_list.end(); i++)
		{
			obj = *i;
			if (obj->getObjectType() == OBJECT_ITEM || obj->getObjectType() == OBJECT_ENEMY)
			{
				if (obj->getPosX() > camera->getCamera().left - 30 && obj->getPosX() < camera->getCamera().left + _ScreenWidth + 30)
				{
					obj->render(camera->getCamera().left, camera->getCamera().top);
				}
			}
		}

		//draw player last
		p_player->render(camera->getCamera().left, camera->getCamera().top);

		
		//Draw score dong
		Score* tempScore;
		list<Score*>::iterator iScore;
		for (iScore = scoreList.begin(); iScore != scoreList.end(); iScore++)
		{
			tempScore = *iScore;
			tempScore->Render(camera->getCamera().left, camera->getCamera().top);
		}

		//draw diem, mang,...
		


		scoreArr[SCORE]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[HEART]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[NUMBER_LIFE]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[TIME]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		scoreArr[WORLD1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);

		infoArr[0]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[2]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[3]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[4]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[5]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[6]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		infoArr[7]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);

		for (int i = 0; i < 8; i++)
		{
			info_death_player[i]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
			info_death_boss[i]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		}
		for (int i = 0; i < p_player->getBlood(); i++)
		{
			info_blood_player[i]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		}
		for (int i = 0; i < boss_blood; i++)
		{
			info_blood_boss[i]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
		}

		switch (p_player->getWeaponVersion())
		{
		case WEAPON_NONE:
			info_weapon[0]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
			break;

		case WEAPON_AXE:
			info_weapon[1]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
			break;

		case WEAPON_DAGGER:
			info_weapon[2]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
			break;

		case WEAPON_HOLY_WATER:
			info_weapon[3]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
			break;

		case WEAPON_BOOMERANG:
			info_weapon[4]->Render(camera->getCamera(), camera->getCamera().left, camera->getCamera().top);
			break;
			
		}

		break;


	}


	_spriteHandler->End();
}


/**--------------------------------------------------------
*Description : Kiem tra phim nhan
*Method name : ProcessInput
*Parameters :  LPDIRECT3DDEVICE9 d3ddv, int t
*Return value :void
--------------------------------------------------------**/
void CastleVaniaGame::ProcessInput(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	//printf("FPS: %f", this->GetFPS());
	
	switch (game_state)
	{
	case GS_MENU:
		{
			int temp = p_input->GetKeyDown();
			switch (temp)
			{
			case DIK_DOWN:
				yPointMenu += 50;
				if (yPointMenu > 420)
				{
					yPointMenu = 270;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_UP:
				yPointMenu -= 50;
				if (yPointMenu < 270)
				{
					yPointMenu = 420;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_RETURN:	//key Enter
				dxAudio->PlaySoundW(fireballSound);
				if (yPointMenu == 270)	//New Game
				{
					num_heart = 5;
					num_life = 3;
					num_score = 0;

					scoreArr[NUMBER_LIFE]->Update(num_life);
					scoreArr[HEART]->Update(num_heart);
					scoreArr[SCORE]->Update(num_score);

					map_current = 11;
					ChangeMap(map_current);
					//ChangeState(GS_PLAYING);

				}
				else if(yPointMenu == 320)	//Load Game
				{
					ChangeState(GS_LOAD);
				}
				else if(yPointMenu == 370)	//Game About
				{
					ChangeState(GS_ABOUT);
				}
				else if (yPointMenu == 420)	//Quit Game
				{
					//PostQuitMessage(0);
					trace(L"Main game is ended!");
					PostMessage(_hWnd,WM_QUIT,0,0);
				}
				break;
			}
		}
		break;

	case GS_PAUSE:
		{
			int temp = p_input->GetKeyDown();
			switch (temp)
			{
			case DIK_DOWN:
				yPointMenu += 50;
				if (yPointMenu > 370)
				{
					yPointMenu = 270;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_UP:
				yPointMenu -= 50;
				if (yPointMenu < 270)
				{
					yPointMenu = 370;
				}
				dxAudio->PlaySoundW(fireballSound);
				break;

			case DIK_RETURN:	//key Enter
				dxAudio->PlaySoundW(fireballSound);
				if (yPointMenu == 270)	//Save Game + Quit
				{
					//Save Game
					ChangeState(GS_SAVE);
				}
				else if(yPointMenu == 320)	//Quit Game - Don't Save
				{
					ChangeState(GS_MENU);
				}
				else if(yPointMenu == 370)	//Don't Quit Game
				{
					ChangeState(GS_PLAYING);
				}
				break;
			}
		}
		break;

	case GS_PLAYING:
		{
			if (p_input->IsKeyDown(DIK_ESCAPE))
			{
				ChangeState(GS_PAUSE);
			}			 
		}
		break;

	case GS_ABOUT:
		if (p_input->IsKeyDown(DIK_ESCAPE))
		{
			ChangeState(GS_MENU);
		}
		break;

	case GS_GAMEOVER:
		if (p_input->IsKeyDown(DIK_ESCAPE))
		{
			ChangeState(GS_MENU);
		}
		break;

	case GS_GAMEWIN:
		if (p_input->IsKeyDown(DIK_ESCAPE))
		{
			ChangeState(GS_MENU);
		}
		break;
	}
}


void CastleVaniaGame::update(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, int Delta)
{
	//////////////////////////////////////////////////////////////////////////

	switch (game_state)
	{
	case GS_MENU:
		break;

	case GS_ABOUT:
		break;

	case GS_PAUSE:
		break;

	case GS_SAVE:
		load_quadtree_obj->numScore = num_score;
		load_quadtree_obj->numLife = num_life;
		load_quadtree_obj->numCoin = num_heart;

		//cap nhat lai vi tri, trang thai, huong di chuyen mario trong map
		load_quadtree_obj->posX_first = (int)(p_player->getPosX());
		load_quadtree_obj->posY_first = (int)(p_player->getPosY() + 2);
		load_quadtree_obj->mario_state = p_player->getObjState();
		load_quadtree_obj->mario_direction = p_player->getDirection();

		quadtree->getObjList(&obj_list_save);
		load_quadtree_obj->saveFileObj("..\\Resources\\Map\\savemapfix.txt", obj_list_save);
		dxAudio->PlaySoundW(fireballSound);
		obj_list_save.clear();

		SaveMap("..\\Resources\\Map\\Save_Map.txt");

		ChangeState(GS_MENU);
		break;

	case GS_NEXT_STAGE:				//Khi đổi màn
		reset_time += Delta;
		if (reset_time > 100)
		{
			reset_time -= 100;
			time_change_map--;
			if (time_change_map <= 0)
			{
				/*if (is_change_map == true)
				{
					map_current = 21;
					is_change_map = false;
				}*/
				if (map_current == 16)
				{
					map_current = 21;
				}
				else
				{
					map_current++;
				}
				scoreArr[WORLD1]->Update(map_current);
				ChangeMap(map_current);
			}
		}
		break;

	case GS_RETURN_STAGE:				//Khi đổi màn
		reset_time += Delta;
		if (reset_time > 100)
		{
			reset_time -= 100;
			time_change_map--;
			if (time_change_map <= 0)
			{
				//subMapToMap = 1;
				//int i = 0;
				ChangeMap(map_current);
			}
		}
		//ChangeMap(map_Current+1);
		break;

	case GS_REPLAY:				//choi lai map
		reset_time += Delta;
		if (reset_time > 100)
		{
			reset_time -= 100;
			time_change_map--;
			if (time_change_map <= 0)
			{
				is_replay = true;
				ChangeMap(map_current);
			}
		}
		//ChangeMap(map_Current+1);
		break;

	case GS_SUB_STAGE:				//choi map phu
		reset_time += Delta;
		if (reset_time > 100)
		{
			reset_time -= 100;
			time_change_map--;
			if (time_change_map <= 0)
			{
				submap = map_current;

				ChangeSubMap(submap);
			}
		}
		break;

	case GS_PLAYING:
		{
			Object* obj;
			list<Object*>::iterator i;

			//Get camera
			RECT rect_camera = camera->getCamera();

			
			//update object list
			obj_list.clear();
			quadtree->getObjFromCamara(rect_camera, &obj_list);

			reset_time += Delta;
			if (reset_time > 500)	//Count game time
			{
				reset_time = 0;//500;
				count_time--;
				scoreArr[TIME]->Update(count_time);
				if (count_time <= 0)
				{
					ChangeState(GS_GAMEOVER);
				}

				count_time_random--;
				if (count_time_random <= 0)
				{
					count_time_random = 10;
					 
					GenerateRandomEnemy(rect_camera);
					 
					
				}
			}

			//add enemy random to obj_list
			for (i = random_enemy_list.begin(); i != random_enemy_list.end(); i++)
			{
				obj = *i;
				obj_list.push_back(obj);
			}
			

			

			//update tiles list
			tiles_list.clear();
			quadtree_tiles->getTilesBgFromCamara(rect_camera, &tiles_list);

			
			//update obj
			for (i = obj_list.begin(); i != obj_list.end(); i++)
			{
				obj = *i;
				if (obj->getIDObjType() == 21 && obj->getPosX() > camera->getCamera().left && obj->getPosX() < camera->getCamera().left + _ScreenWidth)
				{
					obj->update(&obj_list, p_input, Delta);
				}
				if (obj->getIDObjType() != 21 && obj->getPosX() > camera->getCamera().left - 30 && obj->getPosX() < camera->getCamera().left + _ScreenWidth + 30)
				{
					switch (obj->getIDObjType())
					{
					//cap nhat obj co the di chuyen
					case 11:	//ghost
					case 121:	//dog left
					case 122:	//dog right
					case 13:	//bat boss
					//case 14:
					case 15:
					case 16:
					case 17:
					case 18:
						obj->update(&obj_list, p_input, Delta, p_player);
						break;

					default:
						obj->update(&obj_list, p_input, Delta);
						break;
					}

					if (obj->getIDObjType() == 13 || obj->getIDObjType() == 18)	//bat boss
					{
						boss_blood = obj->getBlood();
						
						if (obj->getObjState() == ES_REMOVE)
						{
							is_change_map = true;
						}
					}
					if (obj->getIDObjType() == 44 && is_change_map == true && obj->getObjState() == IS_IDLE)		//Orb
					{
						obj->changeState(IS_EXPLOSION);
						is_change_map = false;
					}

				}			
			}


			//update player
			p_player->update(&obj_list, p_input, Delta);
			//camera update follow player
			camera->update(p_player->getPosition(), width_map);
			simon_axe_version = p_player->getWeaponVersion();
			simon_rod_version = p_player->getRodVersion();
			simon_blood = p_player->getBlood();

			//cap nhat diem, mang, time
			Score* tempScore;
			list<Score*>::iterator iScore;
			//HIEN THI DIEM KHI DAP CHET QUAI, AN DIEM
			scoreRemoveList.clear();
			for (iScore = scoreList.begin(); iScore != scoreList.end(); iScore++)
			{
				tempScore = *iScore;
				tempScore->update(&obj_list, p_input, Delta);
				if (tempScore->getObjState() == IS_IDLE) //die
				{
					delete tempScore;
					scoreRemoveList.push_back(tempScore);
				}
			}
			for (iScore = scoreRemoveList.begin(); iScore != scoreRemoveList.end(); iScore++)
			{
				scoreList.remove(*iScore);
			}


			//music background
			dxAudio->PlaySoundW(background_sound);
		}
		break;

	}//end switch
}


void CastleVaniaGame::GenerateRandomEnemy(RECT camera)
{
	srand(time(0));	//gia tri random thay doi theo time

	Object* obj;
	list<Object*>::iterator i_obj;
	
	for (i_obj = random_enemy_list.begin(); i_obj != random_enemy_list.end(); i_obj++)
	{
		obj = *i_obj;
		if (obj->getObjState() != ES_REMOVE)
		{
			if (obj->getPosX() > camera.left - 30 && obj->getPosX() < camera.left + _ScreenWidth + 30)
			{
				random_enemy_list_remove.push_back(obj);
			}
		}
	}
	random_enemy_list.clear();

	for (i_obj = random_enemy_list_remove.begin(); i_obj != random_enemy_list_remove.end(); i_obj++)
	{
		random_enemy_list.push_back(*i_obj);
	}
	random_enemy_list_remove.clear();


	Object* p_obj;	
	switch (map_current)
	{
	case 11:
		break;

	case 12:
		{
			int count_enemy = 1 + rand() % (2-1+1);

			for (int i = 0; i < count_enemy; i++)
			{
				int direction_enemy = 1 + rand() % (2-1+1);
				if (direction_enemy == 1)	//to left
					p_obj = new Ghost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + camera.right + i*20, 300, 11, 2, direction_enemy);
				else if (direction_enemy == 2)	//to right
					p_obj = new Ghost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left - i*20, 300, 11, 2, direction_enemy);
				random_enemy_list.push_back(p_obj);
			}
		}
		break;

	

	case 13:
	case 15:
		{
			int count_enemy = 1 + rand() % (2-1+1);

			for (int i = 0; i < count_enemy; i++)
			{
				int direction_enemy = 1 + rand() % (2-1+1);
				if (direction_enemy == 1)	//to left
					p_obj = new RedBat(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + camera.right + i*20, 100 + rand() % (300 - 100 + 1), 11, 2, direction_enemy);
				else if (direction_enemy == 2)	//to right
					p_obj = new RedBat(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left - i*20, 100 + rand() % (300 - 100 + 1), 11, 2, direction_enemy);
				random_enemy_list.push_back(p_obj);
			}
		}
		break;
	case 14:
		{
			int count_enemy = 1 + rand() % (2-1+1);

			for (int i = 0; i < count_enemy; i++)
			{
				int direction_enemy = 1 + rand() % (2-1+1);
				if (direction_enemy == 1)	//to left
					p_obj = new RedDragon(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + rand() % (camera.right + i*20), 100, 11, 2, direction_enemy);
				else if (direction_enemy == 2)	//to right
					p_obj = new RedDragon(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + rand() % (camera.right + i*20), 100, 11, 2, direction_enemy);
				random_enemy_list.push_back(p_obj);
			}
		}
		break;

	case 16:
		{
			if (p_player->getPosX() > 10 && p_player->getPosX() < 1120)
			{
				int count_enemy = 1 + rand() % (2-1+1);

				for (int i = 0; i < count_enemy; i++)
				{
					int direction_enemy = 1 + rand() % (2-1+1);
					if (direction_enemy == 1)	//to left
						p_obj = new Ghost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + camera.right + i*20, 300, 11, 2, direction_enemy);
					else if (direction_enemy == 2)	//to right
						p_obj = new Ghost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left - i*20, 300, 11, 2, direction_enemy);
					random_enemy_list.push_back(p_obj);
				}
			}
		}
		break;
	
	case 23:
		{
			if (p_player->getPosX() > 608 && p_player->getPosX() < 1568)
			{
				int count_enemy = 1 + rand() % (2-1+1);

				for (int i = 0; i < count_enemy; i++)
				{
					int direction_enemy = 1 + rand() % (2-1+1);
					if (direction_enemy == 1)	//to left
						p_obj = new HeadGhost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + camera.right + i*20, 300 + rand() % (400 - 300 + 1), 11, 2, direction_enemy);
					else if (direction_enemy == 2)	//to right
						p_obj = new HeadGhost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left - i*20, 300 + rand() % (400 - 300 + 1), 11, 2, direction_enemy);
					random_enemy_list.push_back(p_obj);
				}
			}
		}
		break;

	case 24:
		{
			if (p_player->getPosX() > 50 && p_player->getPosX() < 600)
			{
				int count_enemy = 1 + rand() % (2-1+1);

				for (int i = 0; i < count_enemy; i++)
				{
					int direction_enemy = 1 + rand() % (2-1+1);
					if (direction_enemy == 1)	//to left
						p_obj = new HeadGhost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + camera.right + i*20, 100 + rand() % (300 - 100 + 1), 11, 2, direction_enemy);
					else if (direction_enemy == 2)	//to right
						p_obj = new HeadGhost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left - i*20, 100 + rand() % (300 - 100 + 1), 11, 2, direction_enemy);
					random_enemy_list.push_back(p_obj);
				}
			}
		}
		break;

	case 26:
		{
			if (p_player->getPosX() > 640 && p_player->getPosX() < 1472)
			{
				//int count_enemy = 0 + rand() % (1-0+1);

				for (int i = 0; i < 1; i++)
				{
					int direction_enemy = 1 + rand() % (2-1+1);
					if (direction_enemy == 1)	//to left
						p_obj = new HeadGhost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left + camera.right + i*20, 200 + rand() % (300 - 200 + 1), 11, 2, direction_enemy);
					else if (direction_enemy == 2)	//to right
						p_obj = new HeadGhost(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, camera.left - i*20, 200 + rand() % (300 - 200 + 1), 11, 2, direction_enemy);
					random_enemy_list.push_back(p_obj);
				}
			}
		}
		break;
	

	}
}

void CastleVaniaGame::InfoScore(int score, int x, int y)
{
	num_score += score;
	Score* _tempScore = new Score(d3d->GetDevice(), d3d->GetBackBuffer(), _spriteHandler, x, y);
	_tempScore->score = score;
	scoreList.push_back(_tempScore);
}

void CastleVaniaGame::ChangeState(int state)
{
	game_state = state;
	vector<string> map_vt;

	switch (game_state)
	{
	case GS_MENU:
		xPointMenu = 200;
		yPointMenu = 270;
		break;

	case GS_PLAYING:
		count_time = 400;
		reset_time = 0;
		count_time_random = 10;

		xPointMenu = 200;
		yPointMenu = 270;
		break;

	case GS_LOAD:
		{
			//chua fix
			

			delete load_quadtree_obj;
			load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);

			delete quadtree;
			quadtree = new QuadTree();
			delete quadtree_tiles;
			quadtree_tiles = new QuadTree();

			load_quadtree_obj->obj_list.clear();
			obj_list.clear();



			/*load_quadtree_obj->loadFileObj("..\\Resources\\Map\\savemapfix.txt");
			load_quadtree_obj->loadFileQuadtreeObjToMap("..\\Resources\\Map\\map.txtquadtree.txt");
			load_quadtree_obj->deserializeQuadtree(quadtree);*/




			//map-1-2
			//map_vt = load_quadtree_obj->all_map[12];

			map_vt = LoadSaveMap("..\\Resources\\Map\\Save_Map.txt");

			//obj
			load_quadtree_obj->loadFileObj(map_vt[0]);
			load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
			load_quadtree_obj->deserializeQuadtree(quadtree);

			//Tiles background
			load_quadtree_obj->loadFileTiles(map_vt[2]);
			load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
			load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
			width_map = load_quadtree_obj->width_map;

			int index_map = atoi(map_vt[4].c_str());
			map_current = index_map;
			switch (index_map)
			{
			case 11:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[9]->getFileName());
				break;

			case 12:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[12]->getFileName());
				break;

			case 13:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[19]->getFileName());
				break;

			case 14:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[22]->getFileName());
				break;

			case 15:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[23]->getFileName());
				break;

			case 16:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[24]->getFileName());
				break;

			case 21:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[49]->getFileName());
				break;

			case 22:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[51]->getFileName());
				break;

			case 23:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[53]->getFileName());
				break;

			case 24:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[54]->getFileName());
				break;

			case 25:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[57]->getFileName());
				break;

			case 26:
				LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[60]->getFileName());
				break;
			
			}
			

			//simon
			p_player = NULL;
			p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
			camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
			if (is_replay == true)
			{
				is_replay = false;
			}
			else
			{
				p_player->setWeaponVersion(simon_axe_version);
				p_player->setRodVersion(simon_rod_version);
				p_player->setBlood(simon_blood);
			}













			/*if (is_load_game == 0)
			{
			p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
			}
			else
			{
			p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
			}
			is_load_game++;*/


			num_score = load_quadtree_obj->numScore;
			num_life = load_quadtree_obj->numLife;
			num_heart = load_quadtree_obj->numCoin;
			scoreArr[SCORE]->Update(num_score);
			scoreArr[NUMBER_LIFE]->Update(num_life);
			scoreArr[HEART]->Update(num_heart);

			xPointMenu = 200;
			yPointMenu = 270;
			ChangeState(GS_PLAYING);
		}
		break;

	case GS_ABOUT:
		break;

	case GS_GAMEOVER:
		map_current = 11;	//Neu choi tiep thi choi tu map 1
		break;

	case GS_NEXT_STAGE:
		reset_time = 0;
		time_change_map = 10;
		break;

	case GS_RETURN_STAGE:
		reset_time = 0;
		time_change_map = 10;
		break;

	case GS_REPLAY:
		reset_time = 0;
		time_change_map = 30;

		//Reset simon
		simon_axe_version = WEAPON_NONE;
		simon_rod_version = 1;
		break;

	case GS_SUB_STAGE:
		reset_time = 0;
		time_change_map = 10;
		/*submap = map_Current;
		ChangeSubMap(submap);*/
		break;

	case GS_PAUSE:
		xPointMenu = 200;
		yPointMenu = 270;
		break;

	case GS_SAVE:
		break;
	}
}

void CastleVaniaGame::ChangeMap(int _map)
{

	//Clear random list enemy
	random_enemy_list.clear();
	boss_blood = 8;

	int mario_state = 0;
	map_current = _map;
	
	switch(map_current)
	{
	case 11:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);

		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();

		mario_state = load_quadtree_obj->mario_state;
		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-1-1
		map_vt = load_quadtree_obj->all_map[11];//11

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);
	
		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[9]->getFileName());//9

		/*if (load_quadtree_obj->submapToMap == true)
		{
			load_quadtree_obj->submapToMap = false;
			_player->setObjState(mario_state);

			_player->setPosX(load_quadtree_obj->posX_last);
			_player->setPosY(load_quadtree_obj->posY_last);
		}
		else
		{
			_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);

		}*/

		//simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		//p_player->setPosX(100);
		//p_player->setPosY(500);
		camera->update(p_player->getPosition(), width_map); //cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		break;

	case 12:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);

		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();
		

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-1-2
		map_vt = load_quadtree_obj->all_map[12];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[12]->getFileName());

		//simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		//p_player->setPosX(100);
		//p_player->setPosY(500);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		

		/*if (load_quadtree_obj->submapToMap == true)
		{
			load_quadtree_obj->submapToMap = false;
			_player->setPosX(load_quadtree_obj->posX_last);
			_player->setPosY(load_quadtree_obj->posY_last);			
		}
		else
		{
			_player->setPosX(load_quadtree_obj->posX_first);
			_player->setPosY(load_quadtree_obj->posY_first);			
		}*/
		break;

	case 13:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);

		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();
		

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-1-3
		map_vt = load_quadtree_obj->all_map[13];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[19]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		//p_player->setPosX(100);
		//p_player->setPosY(500);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	case 14:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);

		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();


		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-1-4
		map_vt = load_quadtree_obj->all_map[14];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[22]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		//p_player->setPosX(100);
		//p_player->setPosY(500);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	case 15:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);

		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();


		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-1-5
		map_vt = load_quadtree_obj->all_map[15];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[23]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		//p_player->setPosX(100);
		//p_player->setPosY(500);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	case 16:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);

		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();


		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-1-6
		map_vt = load_quadtree_obj->all_map[16];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[24]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		//p_player->setPosX(100);
		//p_player->setPosY(500);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	//Map 2---------------------------------------------------//
	case 21:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);
		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-2-1
		map_vt = load_quadtree_obj->all_map[21];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[49]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	case 22:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);
		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-2-1
		map_vt = load_quadtree_obj->all_map[22];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[51]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;


	case 23:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);
		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-2-3
		map_vt = load_quadtree_obj->all_map[23];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[53]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	case 24:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);
		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-2-4
		map_vt = load_quadtree_obj->all_map[24];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[54]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	case 25:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);
		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-2-5
		map_vt = load_quadtree_obj->all_map[25];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[57]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	case 26:
		delete load_quadtree_obj;
		load_quadtree_obj = new LoadMap(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), this->_spriteHandler);
		delete quadtree;
		quadtree = new QuadTree();
		delete quadtree_tiles;
		quadtree_tiles = new QuadTree();

		load_quadtree_obj->obj_list.clear();
		obj_list.clear();

		//map-2-6
		map_vt = load_quadtree_obj->all_map[26];

		//obj
		load_quadtree_obj->loadFileObj(map_vt[0]);
		load_quadtree_obj->loadFileQuadtreeObjToMap(map_vt[1]);
		load_quadtree_obj->deserializeQuadtree(quadtree);

		//Tiles background
		load_quadtree_obj->loadFileTiles(map_vt[2]);
		load_quadtree_obj->loadFileQuadtreeTilesToMap(map_vt[3]);
		load_quadtree_obj->deserializeQuadtree(quadtree_tiles);
		width_map = load_quadtree_obj->width_map;
		LoadBackground((char*)ResourceMng::GetInst(this->d3d->GetDevice(), this->d3d->GetBackBuffer())->getMapTexture()[60]->getFileName());

		//Simon
		p_player = NULL;
		p_player = new Simon(this->d3d->GetDevice(), this->d3d->GetBackBuffer(), _spriteHandler, load_quadtree_obj->posX_first, load_quadtree_obj->posY_first, this, load_quadtree_obj->mario_state, load_quadtree_obj->mario_direction);
		camera->update(p_player->getPosition(), width_map);	//cap nhat lai vi tri simon, neu k simon update nhanh qua, dan den bi chet=>game_main chuyen sang REPLAY
		if (is_replay == true)
		{
			is_replay = false;
		}
		else
		{
			p_player->setWeaponVersion(simon_axe_version);
			p_player->setRodVersion(simon_rod_version);
			p_player->setBlood(simon_blood);
		}
		break;

	}




	if(map_current < 27)
		ChangeState(GS_PLAYING);
	else
	{
		ChangeState(GS_GAMEWIN);
		p_player->setPosX(100);
		p_player->setPosY(180);
	}

}


void CastleVaniaGame::ChangeSubMap(int _submap)
{

	subMapToMap = 1;

	submap = _submap;
	switch(submap)
	{
	case 1:	
		load_quadtree_obj->mario_state = p_player->getObjState();
		load_quadtree_obj->mario_direction = p_player->getDirection();

		delete quadtree;
		quadtree = new QuadTree();		
		load_quadtree_obj->obj_list.clear();		
		obj_list.clear();
		load_quadtree_obj->loadFileObj("..\\Resources\\Map\\submap.txt");
		load_quadtree_obj->loadFileQuadtreeObjToMap("..\\Resources\\Map\\submap.txtquadtree.txt");
		load_quadtree_obj->deserializeQuadtree(quadtree);
		load_quadtree_obj->submapToMap = true;		

		p_player->setPosX(load_quadtree_obj->posX_first);
		p_player->setPosY(load_quadtree_obj->posY_first);
		break;

	case 2:		
		delete quadtree;
		quadtree = new QuadTree();
		load_quadtree_obj->obj_list.clear();

		obj_list.clear();
		load_quadtree_obj->loadFileObj("..\\Resources\\Map\\submap.txt");
		load_quadtree_obj->loadFileQuadtreeObjToMap("..\\Resources\\Map\\submap.txtquadtree.txt");
		load_quadtree_obj->deserializeQuadtree(quadtree);

		p_player->setPosX(load_quadtree_obj->posX_first);
		p_player->setPosY(load_quadtree_obj->posY_first);
		break;

	case 3:
		delete quadtree;
		quadtree = new QuadTree();
		load_quadtree_obj->obj_list.clear();
		obj_list.clear();
		load_quadtree_obj->loadFileObj("..\\Resources\\Map\\submap.txt");
		load_quadtree_obj->loadFileQuadtreeObjToMap("..\\Resources\\Map\\submap.txtquadtree.txt");
		load_quadtree_obj->deserializeQuadtree(quadtree);

		p_player->setPosX(load_quadtree_obj->posX_first);
		p_player->setPosY(load_quadtree_obj->posY_first);
		break;
	}


	if(submap < 4)
		ChangeState(GS_PLAYING);
	else 
		ChangeState(GS_GAMEWIN);

}

void CastleVaniaGame::LoadBackground(char* tiles_file)
{
	p_spriteBg = new Sprite(_spriteHandler, ResourceMng::GetInst(d3d->GetDevice(), d3d->GetBackBuffer())->GetTexture((LPWSTR)tiles_file), 0, 0);
}

void CastleVaniaGame::RenderBackground()
{
	//Render background tiles
	TilesObject* p_tiles;
	list<TilesObject*>::iterator iTiles;
	for (iTiles = tiles_list.begin(); iTiles != tiles_list.end(); iTiles++)
	{
		p_tiles = *iTiles;
		int index = p_tiles->id_type;
		p_spriteBg->setIndex(index);
		p_spriteBg->setFrame(index, index);
		p_spriteBg->render(p_tiles->pos_x, _ScreenHeight - p_tiles->pos_y, camera->getCamera().left, camera->getCamera().top);
	}	
}

void CastleVaniaGame::SaveMap(char* file_name)
{
	//Ghi thong tin cac obj xuong file
	ofstream fileStream(file_name);
	Object* obj;
	list<Object*>::iterator iList;

	//ghi dong dau tien, so luong obj, so cot thong tin obj, so diem, so mang, so coin an dc, vi tri dau tien, vi tri tiep theo mario, trang thai mario, huong di chuyen
	int width = 8;
	int height = this->obj_list.size();
	//fileStream<<width<<" "<<height<<" "<<numScore<<" "<<numLife<<" "<<numCoin<<" "<<posX_first<<" "<<posY_first<<" "<<posX_last<<" "<<posY_last<<" "<<mario_state<<" "<<mario_direction<<"\n";

	fileStream<<map_current<<" "<<"..\\Resources\\Map\\savemapfix.txt"<<" "<<map_vt[1]<<" "<<map_vt[2]<<" "<<map_vt[3]<<"\n";
	

	fileStream.close();
}

vector<string> CastleVaniaGame::LoadSaveMap(char* file_name)
{
	string data;
	ifstream fileStream(file_name);
	if (fileStream.is_open())
	{
		while (!fileStream.eof())
		{
			data += fileStream.get();
		}
	}
	vector<string> dataSplit = Utility::split(data, '\n');
	/*string row_header = dataSplit[0];
	vector<string> element_row = Utility::split(row_header, ' ');*/

	/*for (int i = 0; i < dataSplit.size(); i++)
	{*/
		string temp = dataSplit[0];
		vector<string> temp_vector = Utility::split(temp, ' ');

		int key_map = atoi(temp_vector[0].c_str());

		string path_file_obj = temp_vector[1].c_str();
		string path_file_obj_qt = temp_vector[2].c_str();
		string path_file_tile_obj = temp_vector[3].c_str();
		string path_file_tile_obj_qt = temp_vector[4].c_str();

		vector<string> map_vt_temp;
		map_vt_temp.push_back(path_file_obj);
		map_vt_temp.push_back(path_file_obj_qt);
		map_vt_temp.push_back(path_file_tile_obj);
		map_vt_temp.push_back(path_file_tile_obj_qt);

		map_vt_temp.push_back(temp_vector[0].c_str());
		//all_map[key_map] = map_vt;
		
	//}
		map_vt = map_vt_temp;
	return map_vt_temp;
}